//
//  EVDefaultAlertConentView.m
//  EVClub
//
//  Created by LiJieming on 15/7/27.
//  Copyright (c) 2015年 BitRice. All rights reserved.
//

#import "EVDefaultAlertConentView.h"
#import "EVAlertView.h"
#import "Masonry.h"
#import "UIView+EV.h"
#import "NSDictionary+EV.h"
#import "UIImageView+WebCache.h"

@interface EVDefaultAlertConentView () {
    UIImage *_iconImage;
    NSString *_titleText;
    NSString *_commitButtonText;
    NSString *_cancelButtonText;
}

@property (weak, nonatomic) IBOutlet UIImageView *iconImageView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageContainerViewHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconImageContainerBottomOffset;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *detailLabel;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *titleDetailInnerSpace;
@property (weak, nonatomic) IBOutlet UIButton *cancelButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *cancelButtonWidth;
@property (weak, nonatomic) IBOutlet UIButton *commitButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commitButtonWidth;
@property (weak, nonatomic) IBOutlet UIView *buttonSeparatorLine;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *buttonContainerHeight;
@property (weak, nonatomic) IBOutlet UIButton *closeButton;

@end

@implementation EVDefaultAlertConentView

- (UINib *)viewInitNib {
    return [UINib nibWithNibName:@"EVDefaultAlertConentView" bundle:nil];
}

- (instancetype)initWithTitle:(NSString *)title commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton {
    return [self initWithIcon:EVAlertIconWarning title:title commitButton:commitButton cancelButton:cancelButton];
}

- (instancetype)initWithIcon:(EVAlertIcon)icon title:(NSString *)title commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton {
    return [self initWithIcon:icon title:title detail:nil commitButton:commitButton cancelButton:cancelButton];
}

- (instancetype)initWithIcon:(EVAlertIcon)icon title:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton {
    if (self = [super init]) {
        UIImage *iconImage = [[self alertIconImages] ev_nilObjectForKey:@(icon)];
        
        self.iconImageView.image = iconImage;
        self.iconImageContainerViewHeight.constant = iconImage == nil ? 0.0 : 40.0;
        self.iconImageContainerBottomOffset.constant = iconImage == nil ? 0.0 : 13.0;
        
        self.titleLabel.text = title;
        self.detailLabel.text = detail;
        self.titleDetailInnerSpace.constant = (isEmpty(title) || isEmpty(detail)) ? 0.0 : 15.0;
        
        self.titleLabel.preferredMaxLayoutWidth = 280.f;
        self.detailLabel.preferredMaxLayoutWidth = 280.f;
        
        [self.commitButton setTitle:commitButton forState:UIControlStateNormal];
        [self.cancelButton setTitle:cancelButton forState:UIControlStateNormal];
        
        if (commitButton == nil && cancelButton == nil) {
            self.buttonContainerHeight.constant = 0.0;
        }
        else if (commitButton == nil) {
            self.commitButton.hidden = YES;
            self.buttonSeparatorLine.hidden = YES;
            self.cancelButtonWidth.constant = 280.0;
        }
        else if (cancelButton == nil) {
            self.cancelButton.hidden = YES;
            self.buttonSeparatorLine.hidden = YES;
            self.commitButtonWidth.constant = 280.0;
        }
        
        self.closeButton.hidden = !self.showCloseButton;
        self.ev_size = CGSizeMake(280.0, [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
    }
    
    return self;
}

- (instancetype)initWithIconImage:(UIImage *)iconImage title:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton {
    if (self = [self initWithIcon:EVAlertIconCustom title:title detail:detail commitButton:commitButton cancelButton:cancelButton]) {
        self.iconImageView.image = iconImage;
        self.iconImageContainerViewHeight.constant = iconImage == nil ? 0.0 : 40.0;
        
        self.ev_size = CGSizeMake(280.0, [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
    }
    
    return self;
}

- (instancetype)initWithIconImageUrl:(NSURL *)iconImageURL title:(NSString *)title detail:(NSString *)detail commitButton:(NSString *)commitButton cancelButton:(NSString *)cancelButton {
    if (self = [self initWithIcon:EVAlertIconCustom title:title detail:detail commitButton:commitButton cancelButton:cancelButton]) {
        [self.iconImageView sd_setImageWithURL:iconImageURL];
        self.iconImageContainerViewHeight.constant = iconImageURL == nil ? 0.0 : 40.0;
        
        self.ev_size = CGSizeMake(280.0, [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height);
    }
    
    return self;
}

- (NSDictionary *)alertIconImages {
    return @{@(EVAlertIconWarning): [UIImage imageNamed:@"alert_icon_warning"]};
}

- (void)setShowCloseButton:(BOOL)showCloseButton {
    _showCloseButton = showCloseButton;
    
    self.closeButton.hidden = !self.showCloseButton;
}

- (void)setTextAlignment:(NSTextAlignment)textAlignment
{
    _textAlignment = textAlignment;
    
    self.titleLabel.textAlignment = textAlignment;
    self.detailLabel.textAlignment = textAlignment;
}

#pragma mark - Actions
- (IBAction)onCancelButtonTouched:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(TDAlertView:didDismissWithButtonIndex:)]) {
        [self.delegate TDAlertView:nil didDismissWithButtonIndex:EVAlertViewButtonIndexCancel];
    }
}

- (IBAction)onCommitButtonTouched:(id)sender {
    if (self.delegate && [self.delegate respondsToSelector:@selector(TDAlertView:didDismissWithButtonIndex:)]) {
        [self.delegate TDAlertView:nil didDismissWithButtonIndex:EVAlertViewButtonIndexCommit];
    }
}

- (IBAction)onCloseButtonTouched:(id)sender {
    [self onCancelButtonTouched:sender];
}

@end
